package com.eascs.spider.yuqing.common.proj;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 各平台映射关系
 * @author hzp
 *
 */
public enum Platform {

	IFENG_NEWS("凤凰资讯" ,"ifeng_news" ,"news.ifeng*?"),
	BAIDU_NEWS("百度新闻" ,"baidu_news" ,"baidu*?"),
	SINA_NEWS("新浪新闻" ,"sina_news" ,"sina*?"),
	SINA_WEIBO("新浪微博" , "sina_weibo" ,"weibo*?");
	
	private String name;//中文名字
	
	private String platform;//平台名称
	
	private String regex;
	
	private Platform(String name , String platform ,String regex){
		this.platform = platform;
		this.regex = regex;
		this.name = name;
	}
	
	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPlatform() {
		return platform;
	}

	public void setPlatform(String platform) {
		this.platform = platform;
	}

	/**
	 * 根据url获取所属平台
	 * @param url
	 * @return
	 */
	public static Platform getPlatform(String url){
		Platform[] platForms = Platform.values();
		for(Platform p : platForms){
			String regex = p.getRegex();
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(url);
			if(matcher.find())
				return p;
		}
		return null;
	}
	
	public static void main(String[] args) {
		String url = "https://weibo.com/5883540465/FwM5x1jEm";
		Platform platForm = Platform.getPlatform(url);
		System.out.println(platForm.getRegex());
		System.out.println(platForm.getName());
		System.out.println(platForm.getPlatform());
	}
}
